﻿using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using HongShijin.Collections;

using HsjCommon = HongShijin.Common;
using HsjParser = HongShijin.CParser_base;
using HsjExcel = HongShijin.Base.CDataToExcel;

namespace FarmilyEditor
{
	public partial class FormMain : Form
	{
		public OdbcConnection Connection;
		public DataSet DataSet;

		public FormMain()
		{
			this.InitializeComponent();
			//
			XmlDocument xml = new XmlDocument();
			xml.Load(HsjCommon.GetFile("xml"));
			//
			int i;
			XmlNodeList nodes;
			this.设置区域(xml.DocumentElement, null, null);
			if (this.地区.Nodes.Count > 0)
				this.地区.Nodes[0].Expand();
			nodes = xml.DocumentElement.SelectNodes("户主关系");
			for (i = 0; i < nodes.Count; i++)
				this.户主关系.Items.Add(nodes[i].InnerText);
			nodes = xml.DocumentElement.SelectNodes("家庭属性");
			for (i = 0; i < nodes.Count; i++)
				this.家庭属性.Items.Add(nodes[i].InnerText);
			nodes = xml.DocumentElement.SelectNodes("性别");
			for (i = 0; i < nodes.Count; i++)
				this.性别.Items.Add(nodes[i].InnerText);
			nodes = xml.DocumentElement.SelectNodes("疾病类型");
			for (i = 0; i < nodes.Count; i++)
				this.疾病类型.Items.Add(nodes[i].InnerText);
			this.列映射.Items.Add("姓名");
			this.列映射.Items.Add("性别");
			this.列映射.Items.Add("户主姓名");
			this.列映射.Items.Add("与户主关系");
			this.列映射.Items.Add("医疗证");
			this.列映射.Items.Add("出生日期");
			this.列映射.Items.Add("电话");
			this.列映射.Items.Add("身份证");
			this.列映射.Items.Add("是否为慢性病");
			this.列映射.Items.Add("慢性病就诊证号");
			this.列映射.Items.Add("慢性病类型");
			this.列映射.Items.Add("家庭属性");
			this.列映射.Items.Add("完整地址");

			this.地区.AfterSelect += new TreeViewEventHandler(delegate(object sender, TreeViewEventArgs e)
			{
				this.地址地区改变(sender, e);
			});

			this.疾病类型.ItemCheck += new ItemCheckEventHandler(delegate(object sender, ItemCheckEventArgs e)
			{
				this.慢性病证.Enabled =(this.疾病类型.CheckedItems.Count > 1 || e.NewValue == CheckState.Checked);
			});

			this.疾病类型.Leave += new EventHandler(delegate(object sender, EventArgs e)
			{
				if (this.疾病类型.CheckedItems.Count > 0)
					return;

				this.慢性病证_Leave(sender, e);
			});

			this.KeyPress += new KeyPressEventHandler(delegate(object sender, KeyPressEventArgs e)//this.KeyPress
			{
				if (e.KeyChar != '\r' || this.ActiveControl == null)
					return;

				e.Handled = true;
				this.SelectNextControl(this.ActiveControl, true, true, true, true);
			});
			//
			this.出生日期.MinDate = DateTime.Today.AddYears(-128);
			this.出生日期.MaxDate = DateTime.Today;
			this.信息.Tag = this.信息.Text;
		}

		public void Form_Load(object sender, EventArgs e)
		{
			this.Connection = new OdbcConnection("Driver={Microsoft Access Driver (*.mdb)};DBQ=" + HsjCommon.GetFile("mdb"));
			//this.Connection = new OdbcConnection("Provider=Microsoft.Jet.OLEDB.3.5;Data Source=" + HsjCommon.GetFile("mdb"));
			//this.Connection = new OdbcConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HsjCommon.GetFile("mdb"));
			this.Connection.Open();
			CNameObjectCollection noc = new CNameObjectCollection();
			noc.Add("姓名", "本人");
			this.户主.Items.Add(new ListItem("姓名", noc));
			OdbcCommand cmd = new OdbcCommand("select top 100 序列号, 姓名, 电话, 医疗证, 地区代码, 家庭属性, 家庭地址 from 家庭表 where 上级序列 is null or 上级序列 = 0 or 上级序列 = 序列号 order by 序列号 desc;");
			cmd.Connection = this.Connection;
			OdbcDataReader dr = cmd.ExecuteReader();
			while (dr.Read())
			{
				noc = new CNameObjectCollection();
				noc.Add("序列号", dr["序列号"]);
				noc.Add("姓名", dr["姓名"]);
				noc.Add("电话", dr["电话"]);
				noc.Add("医疗证", dr["医疗证"]);
				noc.Add("地区代码", dr["地区代码"]);
				noc.Add("家庭属性", dr["家庭属性"]);
				noc.Add("家庭地址", dr["家庭地址"]);
				this.户主.Items.Add(new ListItem("姓名", noc));
			}
			dr.Close();
			dr = null;
			cmd.Dispose();
			cmd = null;
			this.搜索_Click(sender, e);
			this.清理_Click(this.清理, null);
			//if (this.DataSet.Tables.Count == 0 || this.DataSet.Tables[0].Rows.Count == 0)
			//    return;

			//this.数据列表.Tag = this.DataSet.Tables[0].Rows[0]["序列号"];
			//this.数据列表_CellDoubleClick(this, null);
			//this.数据列表.Tag = null;
			//this.修改.Enabled = false;
		}

		public void Form_Closed(object sender, FormClosedEventArgs e)
		{
			this.DataSet.Dispose();
			this.DataSet = null;
			this.Connection.Close();
			this.Connection.Dispose();
			this.Connection = null;
		}

		public void 搜索_Click(object sender, EventArgs e)
		{
			int i;
			OdbcCommand cmd = new OdbcCommand("Select * From 最低驱动版本家庭视图", this.Connection);
			//OdbcCommand cmd = new OdbcCommand("Select * From 家庭视图", this.Connection);
			if (this.列映射.SelectedItem != null && !string.IsNullOrEmpty(this.关键字.Text))
			{
				cmd.CommandText += string.Format(" where {0} like ?", this.列映射.SelectedItem);
				cmd.Parameters.Add(this.列映射.SelectedItem.ToString(), OdbcType.Text)
					.Value = "%" + this.关键字.Text + "%";
			}
			cmd.CommandText += " order by 序列号 desc;";
			OdbcDataAdapter da = new OdbcDataAdapter(cmd);
			DataSet ds = this.DataSet;
			this.DataSet = new DataSet();
			da.Fill(this.DataSet);
			da.Dispose();
			da = null;
			this.DataSet.Tables[0].PrimaryKey = new DataColumn[] { this.DataSet.Tables[0].Columns["序列号"] };
			this.数据列表.DataSource = this.DataSet.Tables[0];
			this.信息.Text = string.Format(this.信息.Tag + string.Empty, this.DataSet.Tables[0].Rows.Count, 1);
			for (i = 0; i < this.数据列表.Columns.Count; i++)
			{
				if (this.列映射.Items.IndexOf(this.数据列表.Columns[i].DataPropertyName) < 0)
					this.数据列表.Columns[i].Visible = false;
			}
			if (this.数据列表.Rows.Count > 0)
				this.数据列表_CellClick(this.数据列表, new DataGridViewCellEventArgs(0, 0));
			if (ds == null)
				return;

			ds.Dispose();
			ds = null;
		}

		public void 数据列表_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e == null)
				this.数据列表_CellClick(sender, null);
			else
				this.数据列表_CellClick(sender, new DataGridViewCellEventArgs(e.ColumnIndex, e.RowIndex));
		}

		public void 数据列表_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e == null)
				this.数据列表_CellDoubleClick(sender, null);
			else
				this.数据列表_CellDoubleClick(sender, new DataGridViewCellEventArgs(e.ColumnIndex, e.RowIndex));
		}

		public void 数据列表_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			ListItem li;
			if (e == null || e.RowIndex < 0)
				return;

			this.信息.Text = string.Format(this.信息.Tag + string.Empty, this.DataSet.Tables[0].Rows.Count, e.RowIndex + 1);
			DataRow dr = this.DataSet.Tables[0].Rows.Find(this.数据列表.Rows[e.RowIndex].Cells["序列号"].Value);
			object thisID = dr["序列号"];
			object superID = dr["上级序列"];
			object thisName = dr["姓名"];
			object superName = dr["户主姓名"];
			CNameObjectCollection noc = new CNameObjectCollection();
			if (superID.Equals(0) || superID.Equals(thisID))
			{
				noc.Add("序列号", thisID);
				noc.Add("姓名", thisName);
			}
			else
			{
				noc.Add("序列号", superID);
				noc.Add("姓名", superName);
			}
			noc.Add("电话", dr["电话"]);
			noc.Add("医疗证", dr["医疗证"]);
			noc.Add("地区代码", dr["地区代码"]);
			noc.Add("家庭属性", dr["家庭属性"]);
			noc.Add("家庭地址", dr["家庭地址"]);
			li = new ListItem("姓名", noc);
			if (li[0].Equals(0))
				return;

			int i = this.户主.Items.IndexOf(li);
			if (i >= 0)
			{
				if (!this.户主.Enabled)
					return;

				this.户主.SelectedIndex = i;
				return;
			}

			this.户主.Items.Insert(1, li);
			if (!this.户主.Enabled)
				return;

			this.户主.SelectedIndex = 1;
		}

		public void 数据列表_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			int i;
			object o;
			string[] ts;
			ListItem li;
			DataTable dt;
			OdbcDataAdapter da;
			Form frm = sender as Form;
			if (frm != null || e == null)
				o = this.数据列表.Tag;
			else if (e.RowIndex >= 0)
				o = this.数据列表.Rows[e.RowIndex].Cells["序列号"].Value;
			else
				return;

			DataRow dr = this.DataSet.Tables[0].Rows.Find(o);
			if (dr == null)
			{
				da = new OdbcDataAdapter("Select * From 家庭视图 where 序列号 = ?;", this.Connection);
				da.SelectCommand.Parameters.Add("序列号", OdbcType.Int).Value = o;
				dt = new DataTable();
				da.Fill(dt);
				if (dt.Rows.Count == 0)
				{
					this.消息.Text = "数据不存在，可能已被删除！";
					MessageBox.Show(this.消息.Text, "数据不存在", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					return;
				}

				dr = dt.Rows[0];
			}

			this.数据列表.Tag = dr[0];//tag
			this.修改.Enabled = true;
			this.姓名.Tag = dr["姓名"];//tag
			this.姓名.Text = this.姓名.Tag + string.Empty;
			try
			{
				this.性别.Tag = dr["性别"];//tag
				this.性别.Text = this.性别.Tag + string.Empty;
			}
			catch (Exception x)
			{
				this.消息.Text = x.Message;
				//MessageBox.Show(this.消息.Text);
			}
			try
			{
				this.出生日期.Tag = dr["出生日期"];//tag
				this.出生日期.Value = Convert.ToDateTime(this.出生日期.Tag);
			}
			catch (Exception x)
			{
				this.消息.Text = x.Message;
				//MessageBox.Show(this.消息.Text);
			}
			try
			{
				this.数据列表_CellClick(sender, e);
				this.户主.Tag = dr["上级序列"];//tag
				if (this.户主.Tag.Equals(0) || this.户主.Tag.Equals(dr[0]))
				{
					this.户主.SelectedIndex = 0;
				}
				else
				{
					for (i = 0; i < this.户主.Items.Count; i++)
					{
						li = (ListItem)this.户主.Items[i];
						if (li[0].Equals(this.户主.Tag))
						{
							this.户主.SelectedIndex = i;
							this.户主.Text = li.ToString();
							break;
						}
					}
					this.户主关系.Tag = dr["户主关系"];//tag
					this.户主关系.Text = this.户主关系.Tag + string.Empty;
				}
			}
			catch (Exception x)
			{
				this.消息.Text = x.Message;
				//MessageBox.Show(this.消息.Text);
			}
			this.电话.Tag = dr["电话"];//tag
			this.电话.Text = this.电话.Tag + string.Empty;
			this.身份证.Tag = dr["身份证"];//tag
			this.身份证.Text = this.身份证.Tag + string.Empty;
			try
			{
				this.家庭属性.Tag = dr["家庭属性"];//tag
				this.家庭属性.Text = this.家庭属性.Tag + string.Empty;
			}
			catch (Exception x)
			{
				this.消息.Text = x.Message;
				//MessageBox.Show(this.消息.Text);
			}
			try
			{
				this.医疗证.Tag = dr["医疗证"];//tag
				this.医疗证.Text = this.医疗证.Tag + string.Empty;
			}
			catch (Exception x)
			{
				this.消息.Text = x.Message;
				//MessageBox.Show(this.消息.Text);
			}
			try
			{
				this.地区.Tag = dr["地区代码"];//tag
				this.地区.SelectedNode = this.地区.Nodes.Find(this.地区.Tag + string.Empty, true)[0];
			}
			catch (Exception x)
			{
				this.消息.Text = x.Message;
				//MessageBox.Show(this.消息.Text);
			}
			try
			{
				this.疾病类型.Tag = dr["疾病类型"] + string.Empty;//tag
				ts = this.疾病类型.Tag.ToString().Split(new char[] { '\r', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries);
				while (this.疾病类型.CheckedItems.Count > 0)
					this.疾病类型.SetItemChecked(this.疾病类型.Items.IndexOf(this.疾病类型.CheckedItems[0]), false);
				if (ts == null || ts.Length == 0 || string.IsNullOrEmpty(ts[0]))
				{
					this.慢性病证.Tag = string.Empty;//tag
					this.慢性病证.Text = string.Empty;
				}
				else
				{
					for (i = 0; i < ts.Length; i++)
						this.疾病类型.SetItemChecked(this.疾病类型.Items.IndexOf(ts[i]), true);
					this.慢性病证.Tag = dr["慢性病"];//tag
					this.慢性病证.Text = this.慢性病证.Tag + string.Empty;
				}
			}
			catch (Exception x)
			{
				this.消息.Text = x.Message;
				//MessageBox.Show(this.消息.Text);
			}
			this.家庭地址.Tag = dr["家庭地址"];//tag
			this.家庭地址.Text = this.家庭地址.Tag + string.Empty;
			this.完整地址.Tag = dr["完整地址"];//tag
			this.完整地址.Text = this.完整地址.Tag + string.Empty;
			if (this.修改.Visible)//如果修改显示则不是在新窗口中编辑，可以退出
				return;

			if (frm != null)
				return;

			for (i = 0; i < this.OwnedForms.Length; i++)
			{
				if (this.数据列表.Tag.Equals(this.OwnedForms[i].Tag))
				{
					this.OwnedForms[i].Activate();
					return;
				}
			}

			frm = new FormEdit(this);
			frm.ShowInTaskbar = false;
			frm.Show(this);
		}

		public void 添加_Click(object sender, EventArgs e)
		{
			int i;
			string t;
			string prefix;//医疗证格式
			string config;//医疗证格式
			if (!this.验证输入())
			{
				MessageBox.Show(this.消息.Text);
				return;
			}

			OdbcCommand cmd = new OdbcCommand();
			cmd.Connection = this.Connection;
			this.数据列表.Tag = null;
			cmd.CommandText = "select count(0) from 家庭表 where 上级序列 = ? and 姓名 = ?;";
			if (this.户主.SelectedIndex == 0)
				cmd.Parameters.Add("上级序列", OdbcType.Int).Value = 0;
			else
				cmd.Parameters.Add("上级序列", OdbcType.Int).Value = ((ListItem)this.户主.SelectedItem)[0];
			cmd.Parameters.Add("姓名", OdbcType.Text).Value = this.姓名.Text;
			if (HsjParser.ConvertTo<int>(cmd.ExecuteScalar()) > 0)
			{
				this.消息.Text = "输入的家庭成员已存在！";
				MessageBox.Show(this.消息.Text);
				return;
			}

			if (string.IsNullOrEmpty(this.医疗证.Text))
			{
				config = HsjCommon.Configuration.AppSettings.Settings["医疗证格式"].Value;
				i = config.LastIndexOf('Z') + 1;
				if (this.地区.SelectedNode == null)
					prefix = string.Empty;
				else
					prefix = this.地区.SelectedNode.Name;
				if (prefix.Length >= i)
					prefix = prefix.Substring(0, i);
				else
					prefix = prefix.PadRight(i, '0');
				cmd.CommandText = "select count(0) from 家庭表 where left(医疗证, ?) = ?;";
				cmd.Parameters.Clear();
				cmd.Parameters.Add("left", OdbcType.Int).Value = prefix.Length;
				cmd.Parameters.Add("医疗证", OdbcType.Text).Value = prefix;
				i = HsjParser.ConvertTo<int>(cmd.ExecuteScalar()) + 1;
				cmd.CommandText = "select count(0) from 家庭表 where 医疗证 = ?;";
				cmd.Parameters.Clear();
				cmd.Parameters.Add("医疗证", OdbcType.Text);
				do
				{
					t = prefix + i.ToString(config.Substring(prefix.Length));
					cmd.Parameters["医疗证"].Value = t;
					i--;
				}
				while (HsjParser.ConvertTo<int>(cmd.ExecuteScalar()) > 0 && i >= 0);
				this.医疗证.Text = t;
			}

			cmd.CommandText = "insert into 家庭表 (姓名, 性别, 出生日期, 上级序列, 户主关系, 家庭属性, 电话, 身份证, 医疗证, 地区代码, 所在地区, 慢性病, 疾病类型, 家庭地址, 完整地址) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
			cmd.Parameters.Clear();
			cmd.Parameters.Add("姓名", OdbcType.Text).Value = this.姓名.Text;
			cmd.Parameters.Add("性别", OdbcType.Text).Value = this.性别.Text;
			cmd.Parameters.Add("出生日期", OdbcType.DateTime).Value = this.出生日期.Value;
			if (this.户主.SelectedIndex == 0)
				cmd.Parameters.Add("上级序列", OdbcType.Int).Value = 0;
			else
				cmd.Parameters.Add("上级序列", OdbcType.Int).Value = ((ListItem)this.户主.SelectedItem)[0];
			cmd.Parameters.Add("户主关系", OdbcType.Text).Value = this.户主关系.Text;
			cmd.Parameters.Add("家庭属性", OdbcType.Text).Value = this.家庭属性.Text;
			cmd.Parameters.Add("电话", OdbcType.Text).Value = this.电话.Text;
			cmd.Parameters.Add("身份证", OdbcType.Text).Value = this.身份证.Text.Replace("-", string.Empty);
			cmd.Parameters.Add("医疗证", OdbcType.Text).Value = this.医疗证.Text;
			cmd.Parameters.Add("地区代码", OdbcType.Text).Value = this.地区.SelectedNode.Name;
			cmd.Parameters.Add("所在地区", OdbcType.Text).Value = this.地区.SelectedNode.Text;
			if (this.疾病类型.CheckedItems.Count > 0)
			{
				cmd.Parameters.Add("慢性病", OdbcType.Text).Value = this.慢性病证.Text;
				t = string.Empty;
				for (i = 0; i < this.疾病类型.CheckedItems.Count; i++)
				{
					if (i > 0)
						t += "\r\n";
					t += this.疾病类型.CheckedItems[i];
				}
				cmd.Parameters.Add("疾病类型", OdbcType.Text).Value = t;
			}
			else
			{
				cmd.Parameters.Add("慢性病", OdbcType.Text).Value = string.Empty;
				cmd.Parameters.Add("疾病类型", OdbcType.Text).Value = string.Empty;
			}
			cmd.Parameters.Add("家庭地址", OdbcType.Text).Value = this.家庭地址.Text;
			cmd.Parameters.Add("完整地址", OdbcType.Text).Value = this.完整地址.Text;
			i = cmd.ExecuteNonQuery();
			this.消息.Text = string.Format("成功添加记录{0}条！", i);
			MessageBox.Show(this.消息.Text);
			cmd.Dispose();
			if (i == 0)
				return;

			this.清理_Click(sender, e);
			this.搜索_Click(sender, e);
		}

		public void 修改_Click(object sender, EventArgs e)
		{
			int i;
			string t;
			ListItem li = null;
			if (!this.验证输入())
			{
				MessageBox.Show(this.消息.Text);
				return;
			}

			OdbcCommand cmd = new OdbcCommand();
			cmd.Connection = this.Connection;
			if (this.数据列表.Tag == null)// && this.Equals(this.修改)
			{
				this.消息.Text = "请双击一行后再点击此按钮，如果您是要添加一条记录，请点击添加按钮！";
				//MessageBox.Show(this.消息.Text);
				return;
			}

			cmd.CommandText = "update 家庭表 set";
			if (!this.姓名.Text.Equals(this.姓名.Tag))
			{
				cmd.CommandText += " 姓名 = ?,";
				cmd.Parameters.Add("姓名", OdbcType.Text).Value = this.姓名.Text;
			}
			if (!this.性别.Text.Equals(this.性别.Tag))
			{
				cmd.CommandText += " 性别 = ?,";
				cmd.Parameters.Add("性别", OdbcType.Text).Value = this.性别.Text;
			}
			if (!this.出生日期.Value.Equals(this.出生日期.Tag))
			{
				cmd.CommandText += " 出生日期 = ?,";
				cmd.Parameters.Add("出生日期", OdbcType.DateTime).Value = this.出生日期.Value;
			}
			if (this.户主.SelectedIndex != 0)
			{
				if (this.户主.Tag == null || this.户主.Tag.Equals(0) || this.户主.Tag.Equals(this.数据列表.Tag))
				{
					this.消息.Text = "户主不可以更改！";
					MessageBox.Show(this.消息.Text);
					return;
				}

				li = (ListItem)this.户主.SelectedItem;
				if (!li[0].Equals(this.户主.Tag))
				{
					cmd.CommandText += " 上级序列 = ?,";
					cmd.Parameters.Add("上级序列", OdbcType.Int).Value = li[0];
				}
				if (!this.户主关系.Text.Equals(this.户主关系.Tag))
				{
					cmd.CommandText += " 户主关系 = ?,";
					cmd.Parameters.Add("户主关系", OdbcType.Text).Value = this.户主关系.Text;
				}
			}
			if (!this.家庭属性.Text.Equals(this.家庭属性.Tag))
			{
				cmd.CommandText += " 家庭属性 = ?,";
				cmd.Parameters.Add("家庭属性", OdbcType.Text).Value = this.家庭属性.Text;
			}
			if (!this.电话.Text.Equals(this.电话.Tag))
			{
				cmd.CommandText += " 电话 = ?,";
				cmd.Parameters.Add("电话", OdbcType.Text).Value = this.电话.Text;
			}
			if (!this.身份证.Text.Equals(this.身份证.Tag))
			{
				cmd.CommandText += " 身份证 = ?,";
				cmd.Parameters.Add("身份证", OdbcType.Text).Value = this.身份证.Text.Replace("-", string.Empty);
			}
			if (!this.医疗证.Text.Equals(this.医疗证.Tag))
			{
				cmd.CommandText += " 医疗证 = ?,";
				cmd.Parameters.Add("医疗证", OdbcType.Text).Value = this.医疗证.Text;
			}
			if (!this.地区.SelectedNode.Name.Equals(this.地区.Tag))
			{
				cmd.CommandText += " 地区代码 = ?,";
				cmd.Parameters.Add("地区代码", OdbcType.Text).Value = this.地区.SelectedNode.Name;
				cmd.CommandText += " 所在地区 = ?,";
				cmd.Parameters.Add("所在地区", OdbcType.Text).Value = this.地区.SelectedNode.Text;
			}
			if (!this.慢性病证.Text.Equals(this.慢性病证.Tag))
			{
				cmd.CommandText += " 慢性病 = ?,";
				cmd.Parameters.Add("慢性病", OdbcType.Text).Value = this.慢性病证.Text;
			}
			t = string.Empty;
			for (i = 0; i < this.疾病类型.CheckedItems.Count; i++)
			{
				if (i > 0)
					t += "\r\n";
				t += this.疾病类型.CheckedItems[i];
			}
			if (!t.Equals(this.疾病类型.Tag))
			{
				cmd.CommandText += " 疾病类型 = ?,";
				cmd.Parameters.Add("疾病类型", OdbcType.Text).Value = t;
			}
			if (!this.家庭地址.Text.Equals(this.家庭地址.Tag))
			{
				cmd.CommandText += " 家庭地址 = ?,";
				cmd.Parameters.Add("家庭地址", OdbcType.Text).Value = this.家庭地址.Text;
			}
			if (!this.完整地址.Text.Equals(this.完整地址.Tag))
			{
				cmd.CommandText += " 完整地址 = ?,";
				cmd.Parameters.Add("完整地址", OdbcType.Text).Value = this.完整地址.Text;
			}
			if (cmd.Parameters.Count == 0)
			{
				this.消息.Text = "数据没有更改！";
				MessageBox.Show(this.消息.Text);
				return;
			}

			cmd.CommandText = cmd.CommandText.Remove(cmd.CommandText.Length - 1);
			cmd.CommandText += " where 序列号 = ?;";
			cmd.Parameters.Add("序列号", OdbcType.Int).Value = this.数据列表.Tag;
			i = cmd.ExecuteNonQuery();
			this.消息.Text = string.Format("成功修改记录{0}条！", i);
			MessageBox.Show(this.消息.Text);
			cmd.Dispose();
			if (i == 0)
				return;

			if (this.户主.SelectedIndex == 0)
			{
				for (i = 0; i < this.户主.Items.Count; i++)
				{
					li = (ListItem)this.户主.Items[i];
					if (li[0].Equals(this.数据列表.Tag))
						break;
				}
				if (this.户主.Items.Count > i)
				{
					li["姓名"] = this.姓名.Text;
					li["电话"] = this.电话.Text;
					li["医疗证"] = this.医疗证.Text;
					li["地区代码"] = this.地区.SelectedNode.Name;
					li["家庭属性"] = this.家庭属性.Text;
					li["家庭地址"] = this.家庭属性.Text;
				}
			}
			this.搜索_Click(sender, e);
			this.清理_Click(sender, e);
		}

		public void 删除_Click(object sender, EventArgs e)
		{
			int i;
			OdbcCommand cmd = new OdbcCommand();
			cmd.Connection = this.Connection;
			if (this.数据列表.SelectedRows.Count == 0)
			{
				this.消息.Text = "请选择一行！";
				MessageBox.Show(this.消息.Text);
				return;
			}

			this.删除.Tag = this.数据列表.SelectedRows[0].Cells["序列号"].Value;
			cmd.CommandText = "select count(0) from 家庭表 where 序列号 <> 上级序列 and 上级序列 = ?;";
			cmd.Parameters.Add("序列号", OdbcType.Int).Value = this.删除.Tag;
			if (Convert.ToInt32(cmd.ExecuteScalar()) != 0)
			{
				this.消息.Text = "在该户还有其它人员时，不可以删除户主！";
				MessageBox.Show(this.消息.Text);
				return;
			}

			this.消息.Text = "确定要删除当前记录吗？";
			if (MessageBox.Show(this.消息.Text, "删除记录", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
				return;

			cmd.CommandText = "delete from 家庭表 where 序列号 = ?;";
			i = cmd.ExecuteNonQuery();
			this.消息.Text = string.Format("成功删除记录{0}条！", i);
			MessageBox.Show(this.消息.Text);
			cmd.Dispose();
			if (i == 0)
				return;

			if (this.数据列表.Tag == this.删除.Tag)
				this.清理_Click(sender, e);
			for (i = 0; i < this.户主.Items.Count; i++)
			{
				if (((ListItem)this.户主.Items[i])[0].Equals(this.删除.Tag))
				{
					this.户主.Items.RemoveAt(i);
					break;
				}
			}
			this.搜索_Click(sender, e);
		}

		public void 保存_Click(object sender, EventArgs e)
		{
			if (sender == null)
				return;

			if (this.添加.Equals(sender))//添加
			{
				this.添加_Click(sender, e);
				return;
			}

			if (this.修改.Equals(sender))//修改
			{
				this.修改_Click(sender, e);
				return;
			}

			if (this.删除.Equals(sender))//删除
			{
				this.删除_Click(sender, e);
				return;
			}

			this.消息.Text = "未知的操作类型！";
			MessageBox.Show(this.消息.Text);
		}

		public void 清理_Click(object sender, EventArgs e)
		{
			int i;
			bool b;
			string t;
			string[] ts;
			DateTime dt;
			TreeNode[] tns;
			this.数据列表.Tag = null;
			this.修改.Enabled = false;
			//=====================================================================保存后清理
			t = HsjCommon.Configuration.AppSettings.Settings["保存后清理"].Value;
			if (!this.清理.Equals(sender) && !this.新增家庭.Equals(sender))
			{
				if (bool.TryParse(t, out b))
				{
					if (!b)
					{
						this.姓名.Focus();
						return;
					}
				}
				else if (int.TryParse(t, out i))
				{
					if (i == 0)
					{
						this.姓名.Focus();
						return;
					}
				}
			}

			//=====================================================================添加新家庭
			t = HsjCommon.Configuration.AppSettings.Settings["添加新家庭"].Value;
			if (bool.TryParse(t, out b))
				this.新增家庭.Visible = b;
			else if (int.TryParse(t, out i))
				this.新增家庭.Visible = (i != 0);
			this.添加.Visible = !this.新增家庭.Visible;
			this.清理.Visible = !this.新增家庭.Visible;

			//=====================================================================在新窗口中编辑
			t = HsjCommon.Configuration.AppSettings.Settings["在新窗口中编辑"].Value;
			if (bool.TryParse(t, out b))
				this.修改.Visible = !b;
			else if (int.TryParse(t, out i))
				this.修改.Visible = (i == 0);

			t = HsjCommon.Configuration.AppSettings.Settings["姓名"].Value;
			this.姓名.Tag = null;
			this.姓名.Text = t;

			t = HsjCommon.Configuration.AppSettings.Settings["性别"].Value;
			this.性别.Tag = null;
			if (string.IsNullOrEmpty(t))
				this.性别.SelectedItem = null;
			else if (int.TryParse(t, out i))
				this.性别.SelectedIndex = i;
			else
				this.性别.Text = t;

			t = HsjCommon.Configuration.AppSettings.Settings["出生日期"].Value;
			this.出生日期.Tag = null;
			if (DateTime.TryParse(t, out dt))
				this.出生日期.Value = dt;
			else if (int.TryParse(t, out i))
				this.出生日期.Value = DateTime.Today.AddYears(i);
			else
				this.出生日期.Value = DateTime.Today.AddYears(-16);

			t = HsjCommon.Configuration.AppSettings.Settings["户主"].Value;
			this.户主.Tag = null;
			if (string.IsNullOrEmpty(t))
				this.户主.SelectedIndex = 0;
			else if (int.TryParse(t, out i))
				this.户主.SelectedIndex = i;
			else
				this.户主.Text = t;

			t = HsjCommon.Configuration.AppSettings.Settings["户主关系"].Value;
			this.户主关系.Tag = null;
			if (string.IsNullOrEmpty(t))
				this.户主关系.SelectedItem = null;
			else if (int.TryParse(t, out i))
				this.户主关系.SelectedIndex = i;
			else
				this.户主关系.Text = t;

			t = HsjCommon.Configuration.AppSettings.Settings["电话"].Value;
			this.电话.Tag = null;
			this.电话.Text = t;

			if (HsjParser.ConvertTo<bool>(HsjCommon.Configuration.AppSettings.Settings["十五位身份证"].Value))
				this.身份证.Mask = "000000-000000-000";
			else
				this.身份证.Mask = "000000-00000000-000A";
			t = HsjCommon.Configuration.AppSettings.Settings["身份证"].Value;
			this.身份证.Tag = null;
			this.身份证.Text = t;

			t = HsjCommon.Configuration.AppSettings.Settings["家庭属性"].Value;
			this.家庭属性.Tag = null;
			if (string.IsNullOrEmpty(t))
				this.家庭属性.SelectedItem = null;
			else if (int.TryParse(t, out i))
				this.家庭属性.SelectedIndex = i;
			else
				this.家庭属性.Text = t;

			t = HsjCommon.Configuration.AppSettings.Settings["医疗证"].Value;
			this.医疗证.Tag = null;
			this.医疗证.Text = t;
			this.医疗证.Enabled = string.IsNullOrEmpty(HsjCommon.Configuration.AppSettings.Settings["医疗证格式"].Value);

			t = HsjCommon.Configuration.AppSettings.Settings["地区代码"].Value;
			this.地区.Tag = null;
			tns = this.地区.Nodes.Find(t, true);
			if (tns.Length > 0)
				this.地区.SelectedNode = tns[0];

			t = HsjCommon.Configuration.AppSettings.Settings["慢性病证"].Value;
			this.慢性病证.Tag = null;
			this.慢性病证.Text = t;

			t = HsjCommon.Configuration.AppSettings.Settings["疾病类型"].Value;
			ts = t.Split(new char[] { '\r', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries);
			this.疾病类型.Tag = t;
			while (this.疾病类型.CheckedItems.Count > 0)
				this.疾病类型.SetItemChecked(this.疾病类型.Items.IndexOf(this.疾病类型.CheckedItems[0]), false);
			for (i = 0; i < ts.Length; i++)
				this.疾病类型.SetItemChecked(this.疾病类型.Items.IndexOf(ts[i]), true);
			this.慢性病证.Enabled = (this.疾病类型.CheckedItems.Count > 0);

			t = HsjCommon.Configuration.AppSettings.Settings["家庭地址"].Value;
			this.家庭地址.Tag = null;
			this.家庭地址.Text = t;

			t = HsjCommon.Configuration.AppSettings.Settings["完整地址"].Value;
			this.完整地址.Tag = null;
			this.完整地址.Text = t;

			this.姓名.Focus();
		}

		public void 导出_Click(object sender, EventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
			if (this.导出数据库.Equals(sender))
			{
				dialog.Filter = "Access Files(*.mdb)|*.mdb|All Files(*.*)|*.*";
				if (dialog.ShowDialog() != DialogResult.OK)
					return;

				File.Copy(HsjCommon.GetFile("mdb"), dialog.FileName, true);
				this.消息.Text = "数据已成功导出！";
				MessageBox.Show(this.消息.Text);
				return;
			}

			dialog.Filter = "Excel Files(*.xls)|*.xls|XML Files(*.xml)|*.xml|All Files(*.*)|*.*";
			if (dialog.ShowDialog() != DialogResult.OK)
				return;

			try
			{
				FileStream fs = File.Open(dialog.FileName, FileMode.Create, FileAccess.Write, FileShare.Read);
				HsjExcel excel = new HsjExcel(fs);
				excel.DocumentTitle = "新农合家庭成员信息";
				excel.ColumnName = new string[this.列映射.Items.Count];
				for (int i = 0; i < this.列映射.Items.Count; i++)
					excel.ColumnName[i] = this.列映射.Items[i] + string.Empty;
				excel.Export(this.DataSet.Tables[0]);
				fs.Close();
				this.消息.Text = "数据已成功导出！";
				if (MessageBox.Show(this.消息.Text + "\r\n要打开文件进行编辑吗？", "打开文件", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
					return;

				Process.Start(dialog.FileName);
			}
			catch (Exception x)
			{
				this.消息.Text = x.Message;
				MessageBox.Show(this.消息.Text);
			}
		}

		public void 关闭_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		public void 设置区域(XmlNode xn, TreeNodeCollection tns, string parent)
		{
			if (tns == null)
				tns = this.地区.Nodes;
			parent = string.Format("所在地区[@上级='{0}']", parent);
			XmlNodeList nodes = xn.SelectNodes(parent);
			for (int i = 0; i < nodes.Count; i++)
			{
				tns.Add(nodes[i].Attributes["代码"].Value, nodes[i].InnerText);
				this.设置区域(xn, tns[i].Nodes, tns[i].Name);
			}
		}

		public void 地址地区改变(object sender, EventArgs e)
		{
			if (this.地区.SelectedNode == null)
			{
				this.完整地址.Text = this.家庭地址.Text;
				return;
			}

			this.完整地址.Text = this.地区.SelectedNode.FullPath + this.地区.PathSeparator + this.家庭地址.Text;
			if (!this.地区.Equals(sender))
				return;

			this.出生日期_ValueChanged(sender, e);
		}

		private void 出生日期_ValueChanged(object sender, EventArgs e)
		{
			if (this.地区.SelectedNode == null)
				return;

			string t = this.地区.SelectedNode.Name.Substring(0, 6);
			if (HsjParser.ConvertTo<bool>(HsjCommon.Configuration.AppSettings.Settings["十五位身份证"].Value))
				t += this.出生日期.Value.ToString("yyMMdd");
			else
				t += this.出生日期.Value.ToString("yyyyMMdd");
			if (this.身份证.Text.Replace("-", string.Empty).Trim().Length >= t.Length)
				this.身份证.Text = t + this.身份证.Text.Replace("-", string.Empty).Trim().Substring(t.Length);
			else
				this.身份证.Text = t;
		}

		private void 户主_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.户主.SelectedIndex == 0)//是户主
			{
				if (this.户主关系.Items.IndexOf("户主") < 0)
					this.户主关系.Items.Insert(0, "户主");
				this.户主关系.Text = "户主";
				this.家庭属性.SelectedItem = null;
				this.户主关系.Enabled = false;
				this.电话.Enabled = true;
				this.医疗证.Enabled = string.IsNullOrEmpty(HsjCommon.Configuration.AppSettings.Settings["医疗证格式"].Value);
				this.家庭属性.Enabled = true;
				this.家庭地址.Enabled = true;
				this.地区.Enabled = true;
				return;
			}

			if (this.户主关系.Items.IndexOf("户主") >= 0)
				this.户主关系.Items.Remove("户主");
			//this.户主关系.SelectedItem = null;
			ListItem li = (ListItem)this.户主.SelectedItem;
			try
			{
				this.电话.Text = li["电话"] + string.Empty;
				this.医疗证.Text = li["医疗证"] + string.Empty;
				this.家庭属性.SelectedItem = li["家庭属性"];
				this.家庭地址.Text = li["家庭地址"] + string.Empty;
				this.地区.SelectedNode = this.地区.Nodes.Find(li["地区"] + string.Empty, true)[0];
			}
			catch (Exception x)
			{
				this.消息.Text = x.Message;
				//MessageBox.Show(this.消息.Text);
			}
			this.户主关系.Enabled = true;
			this.电话.Enabled = false;
			this.医疗证.Enabled = false;
			this.家庭属性.Enabled = false;
			this.家庭地址.Enabled = false;
			this.地区.Enabled = false;
		}

		public void 各地区列表_SelectedIndexChanged(object sender, EventArgs e)
		{
			ComboBox cmo = sender as ComboBox;
			if (cmo == null)
				return;

			TreeNode[] tns = this.地区.Nodes.Find(((ListItem)cmo.SelectedItem)[0] + string.Empty, true);
			if (tns.Length > 0)
				this.地区.SelectedNode = tns[0];
		}

		private void 慢性病证_Leave(object sender, EventArgs e)
		{
			//if (MessageBox.Show("是否要保存数据？", "保存数据", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
			//    return;

			//if (this.数据列表.Tag == null)
			//    this.保存_Click(this.添加, e);
			//else
			//    this.保存_Click(this.修改, e);
		}

		public void 隐藏地区_Tick(object sender, EventArgs e)
		{
			this.地区.Visible = false;
		}

		public bool 验证输入()
		{
			string t;
			this.姓名.Text = this.姓名.Text.Trim();
			if (string.IsNullOrEmpty(this.姓名.Text))
			{
				this.消息.Text = "姓名不可为空！";
				//MessageBox.Show(this.消息.Text);
				this.姓名.Focus();
				return false;
			}

			if (this.户主.SelectedIndex == 0 || HsjParser.ConvertTo<bool>(HsjCommon.Configuration.AppSettings.Settings["验证所有身份证"].Value))
			{
				t = this.身份证.Text.Replace("-", string.Empty).Trim();
				this.身份证.Text = t;
				if (!Regex.IsMatch(t, "^[0-9]+$") || (t.Length != 15 && t.Length != 18))
				{
					this.消息.Text = "身份证号码必须是十五或十八位的数字！";
					//MessageBox.Show(this.消息.Text);
					this.身份证.Focus();
					return false;
				}
			}

			if (this.性别.SelectedItem == null)
			{
				this.消息.Text = "请选择性别！";
				//MessageBox.Show(this.消息.Text);
				this.性别.Focus();
				return false;
			}

			if (this.户主.SelectedItem == null)
			{
				this.消息.Text = "请选择户主！";
				//MessageBox.Show(this.消息.Text);
				this.户主.Focus();
				return false;
			}

			if (this.户主关系.SelectedItem == null && this.户主.SelectedIndex != 0)
			{
				this.消息.Text = "请选择户主关系！";
				//MessageBox.Show(this.消息.Text);
				this.户主关系.Focus();
				return false;
			}

			this.电话.Text = this.电话.Text.Trim();
			if (!Regex.IsMatch(this.电话.Text, "^[0-9]+$") || (this.电话.Text.Length < 7 && this.电话.Text.Length > 12))
			{
				this.消息.Text = "电话号码必须是七至十二位的数字！";
				//MessageBox.Show(this.消息.Text);
				this.电话.Focus();
				return false;
			}

			if (this.医疗证.Enabled)
			{
				this.医疗证.Text = this.医疗证.Text.Trim();
				if (!Regex.IsMatch(this.医疗证.Text, "^[0-9]+$") || (this.医疗证.Text.Length < 7 && this.医疗证.Text.Length > 12))
				{
					this.消息.Text = "医疗证号必须是七至十二位的数字！";
					//MessageBox.Show(this.消息.Text);
					this.医疗证.Focus();
					return false;
				}
			}

			if (this.家庭属性.SelectedItem == null)
			{
				this.消息.Text = "请选择家庭属性！";
				//MessageBox.Show(this.消息.Text);
				this.家庭属性.Focus();
				return false;
			}

			if (this.疾病类型.CheckedItems.Count == 0)
				return true;

			this.慢性病证.Text = this.慢性病证.Text.Trim();
			if (!Regex.IsMatch(this.慢性病证.Text, "^[0-9]+$") || (this.慢性病证.Text.Length < 7 && this.慢性病证.Text.Length > 12))
			{
				this.消息.Text = "慢性病就诊证号必须是七至十二位的数字！";
				//MessageBox.Show(this.消息.Text);
				this.慢性病证.Focus();
				return false;
			}

			return true;
		}

	}
}
